package offer

import "math"

/*
时间复杂度:O(N)
空间复杂度:O(N)
*/

func minFlipsMonoIncr(s string) int {
	preSum := make([]int, len(s)+1)
	n := len(preSum)
	for i := 1; i < n; i++ {
		preSum[i] = preSum[i-1] + int(s[i-1]-48)
	}

	ans := math.MaxInt32
	for i := 0; i < n; i++ {
		ans = Min(ans, preSum[i]+n-i-1-(preSum[n-1]-preSum[i]))
	}
	return ans
}

// Min 求最小值
func Min(nums ...int) int {
	// 32位整型最大小值
	result := nums[0]
	for _, num := range nums {
		if result > num {
			result = num
		}
	}
	return result
}